<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">
    <meta http-equiv="X-UA-Compatible" content="ie=edge,chrome=1">
    <meta name="referrer" content="never">
    <title>无限滚动</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        ul {
            list-style: none;
        }

        p {
            line-height: 40px;
        }
    </style>
</head>

<body>

    <script type="text/javascript">
        // 无限滚动：当滚动阴影部分接近底部时，往页面添加内容，让其无法到达底部
        var bodyEle = document.body,
            htmlEle = document.documentElement;

        // 进入页面，添加内容，让页面出现滚动条，且滚动阴影部分距离底部至少 > 200
        // 页面高 - 可视高 > 200  while语句
        while (bodyEle.scrollHeight - htmlEle.clientHeight < 200) {
            // var pEle = document.createElement("p");
            // pEle.innerText = "忽如一夜春风来，千树万树梨花开。";
            // bodyEle.append(pEle);
            bodyEle.insertAdjacentHTML("beforeEnd", "<p>忽如一夜春风来，千树万树梨花开。</p>");
        }

        window.onscroll = function () {
            var sct = window.pageYOffset;
            // 滚动条阴影部分与页面底部的距离 = 页面高 - 可视高 - 已卷入高
            if (bodyEle.scrollHeight - htmlEle.clientHeight - sct < 100) {
                var pEle = document.createElement("p");
                pEle.innerText = "沉舟侧畔千帆过，病树前头万木春";
                bodyEle.append(pEle);
            }
        }
    </script>
</body>

</html>